package com.hz.ruoyiequipment.data.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.ruoyiequipment.data.domain.vo.QxEquipment;
import com.hz.ruoyiequipment.data.domain.vo.QxTimeData;
import com.hz.ruoyiequipment.data.mapper.AgrEquipmentMapper;
import com.hz.ruoyiequipment.data.mapper.AgrEquipmentQxMapper;
import com.hz.ruoyiequipment.data.service.AgrEquipmentQxService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: xqd
 * @CreateDate: 2025/03/06/10:45
 * @Description:
 */
@Service
public class ArgEquipmentQxServiceImpl extends ServiceImpl<AgrEquipmentQxMapper, QxEquipment> implements AgrEquipmentQxService {
    @Autowired
    private AgrEquipmentQxMapper agrEquipmentQxMapper;
    @Autowired
    private AgrEquipmentMapper agrEquipmentMapper;

    @Override
    @Transactional
    public AjaxResult updateAgrEquipmentQx(QxEquipment qxEquipment) {
        agrEquipmentQxMapper.updateAgrEquipmentQx(qxEquipment);
        agrEquipmentMapper.updateAgrEquipmentQx(qxEquipment);
        Integer equipmentId = agrEquipmentQxMapper.getEquipmentId(qxEquipment.getEquipmentId());
        if (equipmentId == null){
            if (qxEquipment.getPlotsId() != null){
                agrEquipmentQxMapper.insertAgrEquipmentPlot(qxEquipment);
            }
        } else {
            agrEquipmentQxMapper.updateAgrEquipmentPlot(qxEquipment);
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult delAgrEquipmentQxByEquipmentId(Long equipmentId) {
        agrEquipmentQxMapper.delAgrEquipmentQxByEquipmentId(equipmentId);
        agrEquipmentMapper.delAgrEquipmentByEquipmentId(equipmentId);
        agrEquipmentQxMapper.delAgrEquipmentPlotByEquipmentId(equipmentId);
        return AjaxResult.success();
    }


    // 查询气象实时数据
    @Override
    public AjaxResult getQxTimeData(Long equipmentId) {
         return AjaxResult.success(agrEquipmentQxMapper.getQxTimeData(equipmentId));
    }

    // 查询气象历史数据
    @Override
    public AjaxResult getQxTimeDatas(Long equipmentId) {
        return AjaxResult.success(agrEquipmentQxMapper.getQxTimeDatas(equipmentId));
    }


    @Override
    public AjaxResult getQxTimeDataHead(Long equipmentId) {
        return AjaxResult.success(agrEquipmentQxMapper.getQxTimeDataHead(equipmentId));
    }

    @Override
    public AjaxResult insertQxTimeData(QxTimeData  qxTimeData) {
         agrEquipmentQxMapper.insertQxTimeData(qxTimeData);
         return AjaxResult.success();
    }
}
